﻿Public MustInherit Class ProcessDialog
	Inherits Process

	Public Result As Object
	Protected Shadows Parent As Window

	Dim DialogSize As New Size(-1, -1)
	Dim Form As Form

	Sub New()
		MyBase.New()
		Dim B1 As Bitmap = My.Resources.panelback
		Dim B As Bitmap = New Bitmap(B1.Width, B1.Height)
		DrawImage(Graphics.FromImage(B), B1, Rectangle.Round(B.GetBounds(System.Drawing.GraphicsUnit.Pixel)), 0.8)
		BackgroundImage = B
		BackgroundImageLayout = ImageLayout.Tile
	End Sub

	Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, keyData As System.Windows.Forms.Keys) As Boolean
		If keyData = Keys.F12 Then ClickButton("Close") : Return True
		If keyData = Keys.F4 Then ClickButton("Reset") : Return True
		If keyData = Keys.F8 Then ClickButton("Save") : Return True
		If keyData = Keys.Escape Then ClickButton("Escape") : Return True
		Return MyBase.ProcessCmdKey(msg, keyData)
	End Function

	Private Function DisplayDialog(Optional ByVal Width As Integer = -1, Optional ByVal Height As Integer = -1) As DialogResult
		Form = New Form
		Form.MinimizeBox = False
		Form.MaximizeBox = False
		Form.StartPosition = FormStartPosition.CenterParent
		Name = "ProcessPanel"
		Form.Controls.Add(Me)
		Dock = DockStyle.Fill
		ApplyDesign()
		If Width = -1 Or Height = -1 Then Size = GetSize()
		Region0.Reset()
		If Not FirstControl Is Nothing Then FirstControl.Focus()
		If Width <> -1 Then Form.Size = New Size(Width, Height)
		Invalidate()
		ResetForm(False)
		Form.Text = Text
		Help = InitialHelp
		Return Form.ShowDialog()
	End Function

	Dim Finished As Boolean = False

	Shadows Sub Run(ByVal Parent As Window)
		Me.Parent = Parent
		SuspendLayout()
		GenericEvent(New ProcessEventArgs(EventType.Initialize))
		ResumeLayout()
		If Not Result Is Nothing Then Close() : Exit Sub
		DisplayDialog(DialogSize.Width, DialogSize.Height)
	End Sub

	Sub SetSize(ByVal w As Integer, ByVal h As Integer)
		DialogSize = New Size(w, h)
	End Sub

	Overrides Sub Close()
		If Form Is Nothing Then Exit Sub
		Form.Close()
	End Sub

	Sub ReturnResult(ByVal Result)
		Me.Result = Result
		Close()
	End Sub

End Class
